package BLC

import (
	"flag"
	"fmt"
	"log"
	"os"
)

func (cli *Client) sendCmd(SendCmd *flag.FlagSet) {

	flagFromArg := SendCmd.String("from", "", "我的地址")
	flagToArg := SendCmd.String("to", "", "目标地址")
	flagAmount := SendCmd.String("amount", "", "转账金额")

	err := SendCmd.Parse(os.Args[2:])
	if err != nil {
		log.Panicf(" prarse cmd of send  failed! %v\n ", err)
	}

	//转账
	if SendCmd.Parsed() {

		if *flagFromArg == "" {
			log.Println("原地址不能是空的----")
			PrintUsage()
			os.Exit(1)
			return
		}

		fmt.Println(*flagFromArg)

		if *flagToArg == "" {
			log.Println("目标地址不能是空的----", *flagToArg)
			PrintUsage()
			os.Exit(1)
			return
		}
		fmt.Println("to:  ", *flagToArg)

		if *flagAmount == "" {
			log.Println("金额不能是空的----")
			PrintUsage()
			os.Exit(1)
			return
		}
		cli.send(StrToArray(*flagFromArg), StrToArray(*flagToArg), StrToArray(*flagAmount))
	}
}

//发送交易
func (cli *Client) send(from, to, value []string) {

	if !dbExists() {
		fmt.Println("数据库不存在----")
		os.Exit(1)
	}

	blockChain := GetBlockChainObj()
	defer blockChain.CloseDB()
	blockChain.MineNewBlock(from, to, value)
}
